﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ecommerce
{
    public partial class Form1 : Form
    {
        private List<int> arrArtistID = new List<int>();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["csMusicStore"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionString);
            try
            {
                conn.Open();
                if (chkInsertMode.Checked)
                {
                    SqlCommand cmd = new SqlCommand("insert into Artists (Name) values (@Name)", conn);
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand cmd = new SqlCommand("update Artists set [Name] = @Name where ArtistID = @ArtistID", conn);
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = Convert.ToInt32(txtArtistID.Text);
                    cmd.ExecuteNonQuery();
                }

            }
            finally {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            LoadArtistList();
            chkInsertMode.Checked = true;
            ClearFields();
        }

        private void ClearFields()
        {
            txtArtistID.Text = "";
            txtName.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            chkInsertMode.Checked = true;
            LoadArtistList();
        }

        private void LoadArtistList()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["csMusicStore"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionString);
            try
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter("select * from Artists order by [Name]", conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                lbArtist.Items.Clear();
                arrArtistID.Clear();
                foreach (DataRow dr in dt.Rows)
                {
                    lbArtist.Items.Add(dr["Name"].ToString());
                    arrArtistID.Add(Convert.ToInt32("ArtistID"));
                }
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
        }

        private void lbArtist_DoubleClick(object sender, EventArgs e)
        {
            if (lbArtist.SelectedIndex >= 0)
            {
                int artistID = arrArtistID[lbArtist.SelectedIndex];
                chkInsertMode.Checked = false;
                string connectionString = ConfigurationManager.ConnectionStrings["csMusicStore"]
            }
        }

    }
}
